package com.wgke.bluetooth.utils;

import android.util.Log;

import com.google.gson.Gson;
import com.wgke.bluetooth.beans.CmdInfoBean;
import com.wgke.bluetooth.event.EventReadBean;
import com.wgke.bluetooth.tools.BtMsg;
import com.wgke.bluetooth.ui.BaseActivity;
import com.wgke.bluetooth.vm.ReadWriteVM;

import org.greenrobot.eventbus.EventBus;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.List;

public class Actions {

    public static byte[] test1() {

        int size = 21;
        ByteBuffer buffer = ByteBuffer.allocate(size);
        buffer.order(ByteOrder.LITTLE_ENDIAN); // 小端序


        // 写入数据（小端序）
        buffer.putShort((short) 0x6868);    // 位置 0-1
        buffer.put(2, (byte) 0x10);         // 位置 2
        buffer.put(3, (byte) 0x94);         // 位置 3 148

        buffer.putShort(4, (short) (size - 6));   // 位置 4-5 (长度)

        // 填充数据部分 (位置 6-20)
        buffer.put(6, (byte) 0x01);
        buffer.put(7, (byte) 0x02);
        buffer.put(8, (byte) 0x03);
        buffer.put(9, (byte) 0x02);
        buffer.put(10, (byte) 0x01);

        buffer.put(11, (byte) 0x02);
        buffer.put(12, (byte) 0x03);
        buffer.put(13, (byte) 0x02);
        buffer.put(14, (byte) 0x01);
        buffer.put(15, (byte) 0x02);

        buffer.put(16, (byte) 0x03);
        buffer.put(17, (byte) 0x02);
        buffer.put(18, (byte) 0x01);
        buffer.put(19, (byte) 0x02);
        buffer.put(20, (byte) 0x03);

        // 可选扩展部分（原JS代码中不会执行）
        if (size > 21) {
            buffer.put(21, (byte) 0x01);
        }

        return buffer.array();

    }

    public static void test2() {
        byte[] data = Actions.test1();
        StringBuilder hexString = new StringBuilder();
        for (byte b : data) {
            hexString.append(String.format("%02X ", b));
        }
        Log.e("ReadWriteVM", "readBytes: " + hexString);

        BtMsg msg = new BtMsg(data);
        int cmd = msg.getCmd();
        int length = msg.getLength();
        Log.e("MainActivity", "cmd=" + cmd + "length=" + length);
        msg.moveToNetMsg();
        for (int index = 0; index < length; index++) {
            Log.e("MainActivity", "" + index + "----" + msg.getPos() + "---value:" + msg.getUnit(BtMsg.UINT8));
        }

        //测试输入法
        String str = "68 68 10940F 00 0102030f";
        byte[] b1 = BtMsg.hexStringToByteArray(str);
        if (b1 != null) {
            StringBuilder hexString1 = new StringBuilder();
            for (byte b : b1) {
                hexString1.append(String.format("%02X ", b));
            }
            Log.e("ReadWriteVM", "readBytes2: " + hexString1);
        } else {

            Log.e("ReadWriteVM", "hexStringToByteArray: null");
        }
    }

    public static void test3(BaseActivity c) {
//        byte[] bytes = new byte[]{90, -91, 24, 1, 0, 0, 0, 1, 1, 0, 0, 20, 0, 0, 0, 0, 0, 4, 0, 0};
//        byte[] bytes2 = new byte[]{0, 0, 0, 0, 0, 0, 0, 51, -86, 0};
        byte[] bytes = new byte[]{90, -91, 4, 7, 0, 25, 0, 36, -86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        byte[] bytes2 = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        byte[] header = SpUtil.getUsePolicyHeader();
        ConfigurableByteArrayProcessor processor = new ConfigurableByteArrayProcessor.Builder().setHeader(header[0], header[1])  // 可选，默认就是0x5A,0xA5
                .setFooter(SpUtil.getUsePolicyFooter())              // 可选，默认就是0xAA
                .setUseLengthField(SpUtil.getUsePolicySize())           // 禁用长度字段
                .setUseChecksum(SpUtil.getUsePolicySum())              // 禁用校验和
                .build(packet -> {
                    Log.e("ReadWriteVM", "readBytes2: " + Arrays.toString(packet));
                });
        byte[] b1 = BluetoothGattManager.processAsUnsigned(bytes);
        byte[] b2 = BluetoothGattManager.processAsUnsigned(bytes2);
        Log.e("ReadWriteVM", "readBytes2: " + Arrays.toString(b1));
        Log.e("ReadWriteVM", "readBytes2: " + BtMsg.byteArrayToHexString(b1));
        Log.e("ReadWriteVM", "readBytes2: " + Arrays.toString(b2));
        Log.e("ReadWriteVM", "readBytes2: " + BtMsg.byteArrayToHexString(b2));

        processor.process(bytes);
        processor.process(bytes2);
        int count = 1;
        c.postDelayed((count++) * 100, () -> {
            processor.process(bytes);
            processor.process(bytes2);
        });

        c.postDelayed((count++) * 100, () -> {
            processor.process(bytes);
            processor.process(bytes2);
        });

        c.postDelayed((count++) * 100, () -> {
            processor.process(bytes);
            processor.process(bytes2);
        });

        c.postDelayed((count++) * 100, () -> {
            processor.process(bytes);
            processor.process(bytes2);
        });

        c.postDelayed((count++) * 100, () -> {
            processor.process(bytes);
            processor.process(bytes2);
        });

    }

    public static void a001() {
        Async.getInstance().await(() -> {
            //byte[] bytes = new byte[]{24, 1, 0, 0, 0, 1, 1, 0, 0, 20, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, -86};
            byte[]  bytes = new byte[]{5, 7, 0, 25, 0, 37, -86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

            String json = FileUtil.loadJsFromAssets("parseCmdinfo.json");
            List<CmdInfoBean> cmdInfoBeans = CommonUtil.parseJsonArray(json, CmdInfoBean.class);
            Gson gson = new Gson();

            Log.e("Actions", "gson t: " + gson.toJson(cmdInfoBeans));
            String t = ReadWriteVM.parseDataToInfo(bytes, cmdInfoBeans);

            Log.e("Actions", "a001 t: " + t);
        });

    }
}
